<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Language" content="zh-CN"><link href="stylesheet.css" media="all" rel="stylesheet" type="text/css">
<title>触发器函数</title>
<script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d286c55b63a3c54a1e43d10d4c203e75"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script>
</head><body class="SECT1">
<div>
<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><th colspan="5" align="center" valign="bottom">PostgreSQL 8.2.3 中文文档</th></tr>
<tr><td width="10%" align="left" valign="top"><a href="plpython-funcs.html" accesskey="P">后退</a></td><td width="10%" align="left" valign="top"><a href="plpython.html">快退</a></td><td width="60%" align="center" valign="bottom">章40. PL/Python - Python 过程语言</td><td width="10%" align="right" valign="top"><a href="plpython.html">快进</a></td><td width="10%" align="right" valign="top"><a href="plpython-database.html" accesskey="N">前进</a></td></tr>
</table>
<hr align="LEFT" width="100%"></div>
<div class="SECT1"><h1 class="SECT1"><a name="PLPYTHON-TRIGGER">40.2. 触发器函数</a></h1><a name="AEN41737"></a>
<p>当一个函数用做触发器的时候，字典 <tt class="LITERAL">TD</tt> 包含与触发器相关的数值。触发器行在 <tt class="LITERAL">TD["new"]</tt> 和/或 <tt class="LITERAL">TD["old"]</tt> 里面，具体情况取决于触发器事件。<tt class="LITERAL">TD["event"]</tt> 中包含以字符串形式出现的事件(<tt class="LITERAL">INSERT</tt>, <tt class="LITERAL">UPDATE</tt>, <tt class="LITERAL">DELETE</tt>, <tt class="LITERAL">UNKNOWN</tt>)。<tt class="LITERAL">TD["when"]</tt> 包含 <tt class="LITERAL">BEFORE</tt>, <tt class="LITERAL">AFTER</tt>, <tt class="LITERAL">UNKNOWN</tt> 之一。<tt class="LITERAL">TD["level"]</tt> 包含 <tt class="LITERAL">ROW</tt>, <tt class="LITERAL">STATEMENT</tt>, <tt class="LITERAL">UNKNOWN</tt> 之一。<tt class="LITERAL">TD["name"]</tt> 包含触发器名字，<tt class="LITERAL">TD["table_name"]</tt> 包含触发器发生的表的名字，<tt class="LITERAL">TD["table_schema"]</tt> 包含触发器发生的表的模式，<tt class="LITERAL">TD["name"]</tt> 包含触发器名字，<tt class="LITERAL">TD["relid"]</tt> 包含触发器发生的表的 OID 。如果 <tt class="COMMAND">CREATE TRIGGER</tt> 包括函数参数，那么这些参数可以通过 <tt class="LITERAL">TD["args"][0]</tt> 到 <tt class="LITERAL">TD["args"][(<tt class="REPLACEABLE"><i>n</i></tt>-1)]</tt> 获得。</p>
<p>如果触发器的 <tt class="LITERAL">TD["when"]</tt> 是 <tt class="LITERAL">BEFORE</tt> ，那么你可以从 Python 函数返回 <tt class="LITERAL">None</tt> 或 <tt class="LITERAL">"OK"</tt> 以表示该行没有修改，返回 <tt class="LITERAL">"SKIP"</tt> 以退出事件，或者 <tt class="LITERAL">"MODIFY"</tt> 表示你已经修改了该行。</p>
</div>
<div>
<hr align="LEFT" width="100%">
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td width="33%" align="left" valign="top"><a href="plpython-funcs.html" accesskey="P">后退</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">首页</a></td><td width="33%" align="right" valign="top"><a href="plpython-database.html" accesskey="N">前进</a></td></tr>
<tr><td width="33%" align="left" valign="top">PL/Python 函数</td><td width="34%" align="center" valign="top"><a href="plpython.html" accesskey="U">上一级</a></td><td width="33%" align="right" valign="top">数据库访问</td></tr>
</table>
</div>
</body></html>